package com.zez.controller;


import com.zez.entity.Student;
import com.zez.service.StudentService;
import com.zez.util.PageResult;
import com.zez.util.Re;
import com.zez.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;
    @GetMapping("/list")
    public List<Student> list(){
        return studentService.findAll();
    }

    @GetMapping("/findByPage")
    public PageResult<Student> findByPage(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize){
        System.out.println("1111");
        return studentService.findByPage(page,pageSize);
    }

    @PostMapping("/search")
    public PageResult<Student> search(@RequestParam("page") Integer page,
                                      @RequestParam("pageSize") Integer pageSize,
                                      @RequestBody(required = false) StudentVO studentVo){
        return studentService.search(page,pageSize,studentVo);
    }

    // 4. 保存学生
    @PostMapping("/save")
    public Re save(@RequestBody Student student){
        return studentService.save(student) ? Re.ok().message("添加成功！") : Re.error().message("添加失败！");
    }

}